/*   1:    */ package com.atomikos.util;
/*   2:    */ 
/*   3:    */ import java.io.File;
/*   4:    */ import java.io.FileDescriptor;
/*   5:    */ import java.io.FileInputStream;
/*   6:    */ import java.io.FileNotFoundException;
/*   7:    */ import java.io.FileOutputStream;
/*   8:    */ import java.io.FilenameFilter;
/*   9:    */ import java.io.IOException;
/*  10:    */ 
/*  11:    */ public class VersionedFile
/*  12:    */ {
/*  13:    */   private String baseDir;
/*  14:    */   private String suffix;
/*  15:    */   private String baseName;
/*  16:    */   private long version;
/*  17:    */   private FileInputStream inputStream;
/*  18:    */   private FileOutputStream outputStream;
/*  19:    */   
/*  20:    */   public VersionedFile(String baseDir, String baseName, String suffix)
/*  21:    */   {
/*  22: 72 */     this.baseDir = baseDir;
/*  23: 73 */     this.suffix = suffix;
/*  24: 74 */     this.baseName = baseName;
/*  25: 75 */     resetVersion();
/*  26:    */   }
/*  27:    */   
/*  28:    */   private void resetVersion()
/*  29:    */   {
/*  30: 80 */     this.version = extractLastValidVersionNumberFromFileNames();
/*  31:    */   }
/*  32:    */   
/*  33:    */   private long extractLastValidVersionNumberFromFileNames()
/*  34:    */   {
/*  35: 84 */     long version = -1L;
/*  36: 85 */     File cd = new File(getBaseDir());
/*  37: 86 */     String[] names = cd.list(new FilenameFilter()
/*  38:    */     {
/*  39:    */       public boolean accept(File dir, String name)
/*  40:    */       {
/*  41: 89 */         return (name.startsWith(VersionedFile.this.getBaseName())) && (name.endsWith(VersionedFile.this.getSuffix()));
/*  42:    */       }
/*  43:    */     });
/*  44: 93 */     if (names != null) {
/*  45: 94 */       for (int i = 0; i < names.length; i++)
/*  46:    */       {
/*  47: 95 */         long sfx = extractVersion(names[i]);
/*  48: 96 */         if ((version < 0L) || (sfx < version)) {
/*  49: 97 */           version = sfx;
/*  50:    */         }
/*  51:    */       }
/*  52:    */     }
/*  53:101 */     return version;
/*  54:    */   }
/*  55:    */   
/*  56:    */   private long extractVersion(String name)
/*  57:    */   {
/*  58:106 */     long ret = 0L;
/*  59:107 */     int lastpos = name.lastIndexOf('.');
/*  60:108 */     int startpos = getBaseName().length();
/*  61:109 */     String suffix = name.substring(startpos, lastpos);
/*  62:    */     try
/*  63:    */     {
/*  64:111 */       ret = new Long(suffix).longValue();
/*  65:    */     }
/*  66:    */     catch (NumberFormatException e)
/*  67:    */     {
/*  68:113 */       IllegalArgumentException err = new IllegalArgumentException("Error extracting version from file: " + name);
/*  69:114 */       err.initCause(e);
/*  70:115 */       throw err;
/*  71:    */     }
/*  72:117 */     return ret;
/*  73:    */   }
/*  74:    */   
/*  75:    */   private String getBackupVersionFileName()
/*  76:    */   {
/*  77:122 */     return getBaseUrl() + (this.version - 1L) + getSuffix();
/*  78:    */   }
/*  79:    */   
/*  80:    */   public String getCurrentVersionFileName()
/*  81:    */   {
/*  82:127 */     return getBaseUrl() + this.version + getSuffix();
/*  83:    */   }
/*  84:    */   
/*  85:    */   public String getBaseUrl()
/*  86:    */   {
/*  87:132 */     return this.baseDir + this.baseName;
/*  88:    */   }
/*  89:    */   
/*  90:    */   public String getBaseDir()
/*  91:    */   {
/*  92:137 */     return this.baseDir;
/*  93:    */   }
/*  94:    */   
/*  95:    */   public String getBaseName()
/*  96:    */   {
/*  97:142 */     return this.baseName;
/*  98:    */   }
/*  99:    */   
/* 100:    */   public String getSuffix()
/* 101:    */   {
/* 102:147 */     return this.suffix;
/* 103:    */   }
/* 104:    */   
/* 105:    */   public FileInputStream openLastValidVersionForReading()
/* 106:    */     throws IllegalStateException, FileNotFoundException
/* 107:    */   {
/* 108:163 */     if (this.outputStream != null) {
/* 109:163 */       throw new IllegalStateException("Already started writing.");
/* 110:    */     }
/* 111:164 */     this.inputStream = new FileInputStream(getCurrentVersionFileName());
/* 112:165 */     return this.inputStream;
/* 113:    */   }
/* 114:    */   
/* 115:    */   public FileOutputStream openNewVersionForWriting()
/* 116:    */     throws FileNotFoundException
/* 117:    */   {
/* 118:184 */     if (this.outputStream != null) {
/* 119:184 */       throw new IllegalStateException("Already writing a new version.");
/* 120:    */     }
/* 121:185 */     this.version += 1L;
/* 122:186 */     this.outputStream = new FileOutputStream(getCurrentVersionFileName());
/* 123:187 */     return this.outputStream;
/* 124:    */   }
/* 125:    */   
/* 126:    */   public void discardBackupVersion()
/* 127:    */     throws IllegalStateException, IOException
/* 128:    */   {
/* 129:205 */     if (this.outputStream == null) {
/* 130:205 */       throw new IllegalStateException("No new version yet!");
/* 131:    */     }
/* 132:206 */     String fileName = getBackupVersionFileName();
/* 133:207 */     File temp = new File(fileName);
/* 134:208 */     if ((temp.exists()) && (!temp.delete())) {
/* 135:208 */       throw new IOException("Failed to delete backup version: " + fileName);
/* 136:    */     }
/* 137:    */   }
/* 138:    */   
/* 139:    */   public void close()
/* 140:    */     throws IOException
/* 141:    */   {
/* 142:219 */     resetVersion();
/* 143:220 */     if (this.inputStream != null) {
/* 144:    */       try
/* 145:    */       {
/* 146:222 */         this.inputStream.close();
/* 147:    */       }
/* 148:    */       catch (IOException e) {}finally
/* 149:    */       {
/* 150:227 */         this.inputStream = null;
/* 151:    */       }
/* 152:    */     }
/* 153:230 */     if (this.outputStream != null) {
/* 154:    */       try
/* 155:    */       {
/* 156:232 */         if (this.outputStream.getFD().valid()) {
/* 157:232 */           this.outputStream.close();
/* 158:    */         }
/* 159:    */       }
/* 160:    */       finally
/* 161:    */       {
/* 162:234 */         this.outputStream = null;
/* 163:    */       }
/* 164:    */     }
/* 165:    */   }
/* 166:    */   
/* 167:    */   public long getSize()
/* 168:    */   {
/* 169:241 */     long res = -1L;
/* 170:242 */     File f = new File(getCurrentVersionFileName());
/* 171:243 */     res = f.length();
/* 172:244 */     return res;
/* 173:    */   }
/* 174:    */ }


/* Location:           C:\myfile\mysoft\apache-maven-3.5.0-bin\apache-maven-3.5.0\myrep\com\atomikos\atomikos-util\3.7.0\atomikos-util-3.7.0.jar
 * Qualified Name:     com.atomikos.util.VersionedFile
 * JD-Core Version:    0.7.0.1
 */